//
//  FZH_SearchBar.swift
//  FirstSwiftProduct
//
//  Created by 知合金服-Mini on 2017/12/19.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

class FZH_SearchBar: UISearchBar {

    var searchBarColor = UIColor.white
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //1. 设置背景颜色
        //设置背景图是为了去掉上下黑线
//        self.backgroundImage = UIImage.init()
        // 设置SearchBar的颜色主题
//        searchBarColor = kThemeColor
//        self.barTintColor = UIColor.
        self.showsCancelButton = true
        removeSearchSubView()
//        let uiButton = self.value(forKey: "cancelButton") as! UIButton
//        uiButton.setTitle("搜索", for: .normal)
//        uiButton.setTitleColor(UIColor.orange,for: .normal)
        self.setCancelButtonTitle(title: "有毛病")
        
    }
    
    func removeSearchSubView()  {
        let searchField:UITextField = self.value(forKey: "searchField") as! UITextField
        searchField.backgroundColor = .white
        searchField.layer.cornerRadius = 5
//        searchField.layer.borderColor = RGBColor(r: 247, g: 75, b: 31).cgColor
        searchField.layer.borderColor = UIColor.gray.cgColor
        searchField.layer.borderWidth = 1
        searchField.layer.masksToBounds = true
        print(searchField)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    let attributes = [
        NSForegroundColorAttributeName : UIColor.orange,
        NSFontAttributeName : UIFont.systemFont(ofSize: 17)
    ]
    func setCancelButtonTitle(title:String)  {
       
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
        self.setValue(title, forKeyPath: "cancelButton.title")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
